﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cinser.Common.Silverlight.DT
{
    /*
     *  定义行，保存表的数据
     * */
    public class DMPDataRow
    {
        /// <summary>
        /// 构造函数,声明为internal
        /// </summary>
        /// <param name="columnCollection">构造规则</param>
        internal DMPDataRow(DMPColumnCollection columnCollection)
        {
            if (columnCollection != null)
            {
/*                this.columnCollection = columnCollection;*/
                this.table = columnCollection.Table;
                foreach (DMPDataColumn column in columnCollection.listColumn)
                {
                    listData.Add(column.DefaultValue);
                }
            }
            else throw new InvalidOperationException("value is null");
        }

        #region 方法


        //删除某列,用于列的删除
        internal void Remove(int index)
        {
            listData.RemoveAt(index);
        }


        //添加某列,用于列的添加
        /// <summary>
        /// 将object插入到ListData指定位置
        /// </summary>
        /// <param name="obj">object</param>
        /// <param name="index">位置索引</param>
        internal void InsertTo(object obj, int index)
        {
            listData.Insert(index, obj);
        }

        /// <summary>
        /// 设置关联表
        /// </summary>
        /// <param name="table"></param>
        internal void SetTable(DMPDataTable table)
        {
            if (table!=null)
            {
                this.table = table;
            }
        }
        #endregion

        #region 索引
        /// <summary>
        /// 通过列对象访问该行数据
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        public object this[DMPDataColumn column]
        {
            get
            {
                if (column != null)
                {
                    int index = this.table.Columns.InedxOf(column);
                    if (index==-1)
                    {
                        throw new InvalidOperationException("can't find this column");
                    }
                    return listData[index];
                }
                else throw new InvalidOperationException("null");
            }
            set
            {
                if (column == null)
                {
                    throw new InvalidOperationException("null");
                }
                int index = this.table.Columns.InedxOf(column);
                if (index == -1)
                {
                    throw new InvalidOperationException("can't find this column");
                }
                if (value.GetType() == this.table.Columns[index].DataType)
                {
                    listData[index] = value;
                }
            }
        }

        /// <summary>
        /// 通过列名访问该行数据
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public object this[string name]
        {
            get
            {
                if (name != null)
                {
                    int index = this.table.Columns.InedxOf(name);
                    return listData[index];
                }
                else throw new InvalidOperationException("null");
            }
            set
            {
                if (name==null)
                {
                    throw new InvalidOperationException("null");
                }
                int index = this.table.Columns.InedxOf(name);
                if (index == -1)
                {
                    throw new InvalidOperationException("can't find this column");
                }
                if (value.GetType() == table.Columns[index].DataType)
                {
                    listData[index] = value;
                }
            }
        }

        /// <summary>
        /// 通过索引访问该行数据
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public object this[int index]
        {
            get
            {
                return listData[index];
            }
            set 
            {
                if (value.GetType() == this.table.Columns[index].DataType)
                {
                    listData[index] = value;
                }
            }
        }
        #endregion
        /// <summary>
        /// 该行所属表
        /// </summary>
        private DMPDataTable table;
        public DMPDataTable Table
        {
            get 
            {
                return table;
            }
        }

        /// <summary>
        /// 该行数据量
        /// </summary>
        public int Count
        {
            get
            {
                return listData.Count;
            }
        }

        /// <summary>
        /// 该行数据格式
        /// </summary>
       // private ColumnCollection columnCollection;
        public DMPColumnCollection DataRowState
        {
            get
            {
                return this.table.Columns;
            }
        }
        private List<object> listData = new List<object>();
    }
}
